package com.kucai.yifaqapp.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.kucai.yifaqapp.R;
import com.kucai.yifaqapp.dagger.component.fragment.DaggerGetMoneyFragmentComponent;
import com.kucai.yifaqapp.dagger.module.fragment.GetMoneyFragmentModule;
import com.kucai.yifaqapp.global.Info;
import com.kucai.yifaqapp.model.network.entity.res.Pay_BanKConformBean;
import com.kucai.yifaqapp.model.network.entity.res.Pay_BankCard;
import com.kucai.yifaqapp.presenter.fragment.GetMoneyFragmentPresenter;
import com.kucai.yifaqapp.ui.activity.GetMoneyActivity;
import com.kucai.yifaqapp.ui.activity.Z_BindBankCardActivity;
import com.kucai.yifaqapp.ui.view.GetMoneyFragmentView;
import com.kucai.yifaqapp.util.EventBus.MessageEvent;
import com.kucai.yifaqapp.util.Md5;
import com.kucai.yifaqapp.widget.AlertDialogIos;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;

/**
 * 取款
 */
public class GetMoneyFragment extends BaseFragment<GetMoneyFragmentPresenter> implements GetMoneyFragmentView {
    @BindView(R.id.conform)
    Button conform;
    @BindView(R.id.cardinfo)
    TextView cardinfo;
    @BindView(R.id.jine)
    EditText jine;
    @BindView(R.id.mima)
    EditText mima;
    private Pay_BankCard payBankCard;
    @BindView(R.id.tiqu)
    LinearLayout tiqu;
    @BindView(R.id.bind)
    LinearLayout bind;
    @BindView(R.id.bindcard)
    Button bindcard;
    @BindView(R.id.lowmoney)
    TextView lowmoney;

    private String cardNo;
    private int lowestmoney;

    @Override
    protected int setContentView() {
        return R.layout.fragment_getmoney;
    }

    @Override
    protected void initDagger() {
        DaggerGetMoneyFragmentComponent.builder()
                .getMoneyFragmentModule(new GetMoneyFragmentModule(this))
                .build().inject(this);

    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        initView();
        initData();
    }

    private void initData() {
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void Event(MessageEvent messageEvent) {
        //然后再访问网络请求
        if (Info.user != null) {
            mPresenter.getQukuannumber(Info.user.token);
        }
    }

    @Override
    protected void ChangeSkin() {

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    private void initView() {
        conform.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (jine.getText().toString().isEmpty()) {
                    Toast.makeText(getActivity(), "请输入金额", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (Double.parseDouble(jine.getText().toString()) < lowestmoney) {
                    Toast.makeText(getActivity(), "输入金额不能小于" + lowestmoney, Toast.LENGTH_SHORT).show();
                    return;
                }
                if (mima.getText().toString().isEmpty()) {
                    Toast.makeText(getActivity(), "请输入取款密码", Toast.LENGTH_SHORT).show();
                    return;
                }
                //确认提交
                if (cardNo.isEmpty()) {
                    Toast.makeText(getActivity(), "网络较差，请重试", Toast.LENGTH_SHORT).show();
                    return;
                }
                mPresenter.getConform(Info.user.token, jine.getText().toString().trim(), Md5.toMd5(mima.getText().toString()), cardNo);
                jine.setText("");
                mima.setText("");
            }
        });
    }

    @Override
    public void getCard(Pay_BankCard card) {
        payBankCard = card;
        //如果没有绑定卡 提示绑卡
        try {
            if (card.getData().getBankName() == null) {
                tiqu.setVisibility(View.GONE);
                bind.setVisibility(View.VISIBLE);
                bindcard.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //开始绑卡
                        Intent intent = new Intent(getActivity(), Z_BindBankCardActivity.class);
                        startActivity(intent);
                    }
                });
            } else {
                tiqu.setVisibility(View.VISIBLE);
                bind.setVisibility(View.GONE);
                lowestmoney = card.getData().getLowRechargeMoney();
                lowmoney.setText("单笔下限：" + card.getData().getLowRechargeMoney());
                cardinfo.setText(card.getData().getBankName() + "   " + card.getData().getCardNo() + "   " + card.getData().getFullName());
                String[] string = card.getData().getCardNo().split("尾号");
                for (int i = 0; i < string.length; i++) {
                    cardNo = string[i];
                }
            }
        } catch (Exception e) {

        }
    }

    @Override
    public void getConform(Pay_BanKConformBean bean) {
        Toast.makeText(getActivity(), bean.getMsg() + "", Toast.LENGTH_SHORT).show();
    }

    private AlertDialogIos myDialog;

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            //刷新操作
            if (Info.user == null) {
                return;
            }
            //这里开始请求网络
            if (!Info.user.hasFundPwd) {
                tiqu.setVisibility(View.GONE);
                bind.setVisibility(View.GONE);
                myDialog = new AlertDialogIos(getActivity()).builder();
                myDialog.setGone().setMsg("是否设置密码").setNegativeButton("取消", R.color.skyblue, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        myDialog.dismiss();
                    }
                }).setPositiveButton("确定", R.color.skyblue, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        myDialog.dismiss();
                        Intent intent = new Intent(getActivity(), GetMoneyActivity.class);
                        startActivity(intent);
                    }
                }).show();
            } else {
                if (Info.user != null) {
                    mPresenter.getQukuannumber(Info.user.token);
                }
            }
        }
    }
}
